//
//  BWFVisitorView.swift
//  网易新闻项目
//
//  Created by 薄文飞 on 16/3/5.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit
import SnapKit
class BWFVisitorView: UIView {
    
    
    var callBack :(() -> ())?
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        setUpUi()
        startAnima()
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    func setPage(imageNmage : String? , textLabel :String?) {
        
        if imageNmage == nil {
            
            startAnima()
            
        } else {
            
            wheeiImage.hidden = true
            iconImage.image = UIImage(named: imageNmage!)
            
            label.text = textLabel
        }
        
    }
    
    //转动动画
    
    func startAnima() {
        let ani = CABasicAnimation(keyPath: "transform.rotation")
        
        ani.toValue = 2 * M_PI
        
        ani.duration = 20
        
        ani.repeatCount = MAXFLOAT
        
        ani.removedOnCompletion = false
        wheeiImage.layer.addAnimation(ani, forKey: nil)
        
    }
    func setUpUi() {
        
        backgroundColor = UIColor(white: 237/255, alpha: 1)
        
        addSubview(wheeiImage)
        addSubview(maskIconView)
        addSubview(iconImage)
        
        addSubview(label)
        addSubview(registerBtn)
        addSubview(loginBtn)
        
//        // 图片约束
//        iconImage.translatesAutoresizingMaskIntoConstraints = false
//        
//        addConstraint(NSLayoutConstraint(item: iconImage, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1, constant: 0))
//        addConstraint(NSLayoutConstraint(item: iconImage, attribute: .CenterY, relatedBy: .Equal, toItem: self, attribute: .CenterY, multiplier: 1, constant: 0))
        
        iconImage.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(self.snp_centerX)
            make.centerY.equalTo(self.snp_centerY)
            
        }
//        
//        // 转动图片约束
//        wheeiImage.translatesAutoresizingMaskIntoConstraints = false
//        
//        addConstraint(NSLayoutConstraint(item: wheeiImage, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1, constant: 0))
//        addConstraint(NSLayoutConstraint(item: wheeiImage, attribute: .CenterY, relatedBy: .Equal, toItem: self, attribute: .CenterY, multiplier: 1, constant: 0))
        wheeiImage.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(iconImage.snp_centerX)
            make.centerY.equalTo(iconImage.snp_centerY)
        }
        
        // 文字约束
//        label.translatesAutoresizingMaskIntoConstraints = false
//        
//        addConstraint(NSLayoutConstraint(item: label, attribute: .CenterX, relatedBy: .Equal, toItem: wheeiImage, attribute: .CenterX, multiplier: 1, constant: 0))
//        addConstraint(NSLayoutConstraint(item: label, attribute: .Top, relatedBy: .Equal, toItem: wheeiImage, attribute: .Bottom, multiplier: 1, constant: 0))
//        addConstraint(NSLayoutConstraint(item: label, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 230))
//        
        
        label.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(wheeiImage.snp_centerX)
            make.top.equalTo(wheeiImage.snp_bottom)
            make.width.equalTo(230)
            
        }
        
        label.sizeToFit()
        
        
        
        // 注册按钮约束
//        loginBtn.translatesAutoresizingMaskIntoConstraints = false
//        
//        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Trailing, relatedBy: .Equal, toItem: label, attribute: .Trailing, multiplier: 1, constant: 0))
//        
//        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Top, relatedBy: .Equal, toItem: label, attribute: .Bottom, multiplier: 1, constant: 15))
//        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Width, relatedBy: .Equal, toItem: nil , attribute: .NotAnAttribute, multiplier: 1, constant: 50))
        
        loginBtn.snp_makeConstraints { (make) -> Void in
            make.trailing.equalTo(label.snp_trailing)
            make.top.equalTo(label.snp_bottom).offset(15)
            make.width.equalTo(50)
        }
        
//        // 登录按钮约束
//        registerBtn.translatesAutoresizingMaskIntoConstraints = false
//        
//        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Leading, relatedBy: .Equal, toItem: label, attribute: .Leading, multiplier: 1, constant: 0))
//        
//        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Top, relatedBy: .Equal, toItem: label, attribute: .Bottom, multiplier: 1, constant: 15))
//        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Width, relatedBy: .Equal, toItem: nil , attribute: .NotAnAttribute, multiplier: 1, constant: 50))
        
        registerBtn.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(label.snp_leading)
            make.top.equalTo(label.snp_bottom).offset(15)
            make.width.equalTo(50)
        }
        // 遮罩
//        maskIconView.translatesAutoresizingMaskIntoConstraints = false
//        addConstraint(NSLayoutConstraint(item: maskIconView, attribute: .CenterY, relatedBy: .Equal, toItem: self, attribute: .CenterY, multiplier: 1, constant: -10))
//        addConstraint(NSLayoutConstraint(item: maskIconView, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1, constant: 0))
        
        maskIconView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
        }
    }
    
    //懒加载图片，文字。登录和注册按钮
    private lazy var iconImage :UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    //懒加载动态动画
    private lazy var wheeiImage :UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    private lazy var label : UILabel = UILabel(text: "关注一些人，回这里看看有什么惊喜", textColor: UIColor.blackColor())
    
    //登录按钮懒加载
    lazy var loginBtn :UIButton = {
        
        let btn = UIButton()
        
                btn.addTarget(self, action: "loginDidClick", forControlEvents: .TouchUpInside)
        
        btn.setBackgroundImage(UIImage(named: "common_button_white"), forState: .Normal)
        btn.titleLabel?.font = UIFont.systemFontOfSize(20)
        btn.setTitleColor(UIColor.blackColor(), forState: .Normal)
        btn.setTitle("登录", forState: .Normal)
        return btn
    }()
    
    //注册按钮懒加载
    lazy var registerBtn :UIButton = {
        
        let btn = UIButton()
        
        btn.addTarget(self, action: "loginDidClick", forControlEvents: .TouchUpInside)
        
        btn.setBackgroundImage(UIImage(named: "common_button_white"), forState: .Normal)

        btn.setTitleColor(UIColor.blackColor(), forState: .Normal)
        btn.titleLabel?.font = UIFont.systemFontOfSize(20)
        btn.setTitle("注册", forState: .Normal)
        return btn
    }()
    
    // 遮罩
    private lazy var maskIconView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    func loginDidClick() {
        
        callBack?()
    }
    
}
